package com.huawei.app.demo.util;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Environment;
import android.util.Log;

/**
 * 日志工具类
 *
 * @since 2021-01-06
 */
public class LogUtils {

    private static String logFileName = "ideahub-demo.log";
    private static final int MAX_SIZE = 20 * 1024 * 1024; // 20M

    private static LogUtils instance = null;
    private PrintWriter printWriter;
    private boolean verboseEnabled = false;
    private boolean debugEnabled = true;

    public static synchronized LogUtils getInstance() {
        if (instance == null) {
            instance = new LogUtils();
        }
        return instance;
    }

    private LogUtils() {
        init();
    }

    private void init() {
        try {
            String logFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + logFileName;
            File logFile = new File(logFilePath);
            if (logFile.exists() && logFile.length() >= MAX_SIZE) {
                logFile.delete();
            }

            printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(logFile, true), "UTF-8"));
        } catch (Exception e){
            Log.e("LogUtils", e.getMessage());
        }
    }

    private String getTimestamp() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        return format.format(new Date());
    }

    public void v(String tag, String msg) {
        if (!verboseEnabled) {
            return;
        }
        if (printWriter == null) {
            init();
        }

        Log.v(tag, msg);
        if (printWriter != null) {
            printWriter.println(getTimestamp() + " VERBOSE  " + tag + " " + msg);
            printWriter.flush();
        }
    }

    public void d(String tag, String msg) {
        if (!debugEnabled) {
            return;
        }
        if (printWriter == null) {
            init();
        }

        Log.d(tag, msg);
        if (printWriter != null) {
            printWriter.println(getTimestamp() + " DEBUG  " + tag + " " + msg);
            printWriter.flush();
        }
    }

    public void i(String tag, String msg) {
        if (printWriter == null) {
            init();
        }

        Log.i(tag, msg);
        if (printWriter != null) {
            printWriter.println(getTimestamp() + " INFO  " + tag + " " + msg);
            printWriter.flush();
        }
    }

    public void e(String tag, String msg) {
        if (printWriter == null) {
            init();
        }

        Log.e(tag, msg);
        if (printWriter != null) {
            printWriter.println(getTimestamp() + " ERROR " + tag + " " + msg);
            printWriter.flush();
        }
    }

    public void e(String tag, String msg, Throwable t) {
        if (printWriter == null) {
            init();
        }

        if (t != null) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            t.printStackTrace(new PrintStream(out));
            msg += System.lineSeparator() + out.toString();
        }
        Log.e(tag, msg);
        if (printWriter != null) {
            printWriter.println(getTimestamp() + " ERROR " + tag + " " + msg);
            printWriter.flush();
        }
    }

    public void close() {
        if (printWriter != null) {
            printWriter.close();
        }
    }

    public void closeQuietly(AutoCloseable closeable) {
        if (closeable == null) {
            return;
        }
        try {
            closeable.close();
        } catch (Exception e) {
            e("closeQuietly", e.getMessage());
        }
    }
}
